package com.bicon.botu.account.api;

import com.bicon.botu.account.common.BoTuOperation;
import com.bicon.botu.account.common.utils.APILevel;
import com.bicon.botu.account.dto.UserInfoParam;
import com.bicon.botu.account.service.IUserInfoService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 个人中心api接口管理
 */
@Slf4j
@RestController
@RequestMapping("/api/userinfo")
public class UserInfoApi extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 新增用户
     * @param userInfoParam
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult createUserInfo(@RequestBody UserInfoParam userInfoParam) {
        if(StringUtils.isEmpty(userInfoParam)){
            return new BaseResult(Constants.RESPONSE_CODE_400,  Constants.RESPONSE_MESSAGE_400);

        }
        //用户ID、和账号不能为空
        if(StringUtils.isEmpty(userInfoParam.getId()) || StringUtils.isEmpty(userInfoParam.getPhone())){
            return new BaseResult(Constants.RESPONSE_CODE_400,  Constants.RESPONSE_MESSAGE_400);

        }
        //新增
        if(userInfoService.createUserInfo(userInfoParam)){
            return sendResult200();
        }
        return sendResult500();
    }


    /**
     * 修改用户资料
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult updateUserInfo(@RequestParam String userId,@RequestParam(required = false) String name,@RequestParam(required = false) String nick,
                                     @RequestParam(required = false) String largeImg,@RequestParam(required = false)String smallImg,
                                     @RequestParam(required = false) Integer gender,@RequestParam(required = false) String birthday) {
        UserInfoParam userInfoParam = new UserInfoParam();
        if(!StringUtils.isEmpty(userId)){
            userInfoParam.setId(userId);
        }else {
            return new BaseResult(Constants.RESPONSE_CODE_400,  Constants.RESPONSE_MESSAGE_400);
        }
        if(!StringUtils.isEmpty(name)){
            userInfoParam.setName(name);
        }
        if(!StringUtils.isEmpty(nick)){
            userInfoParam.setNick(nick);
        }
        if(!StringUtils.isEmpty(largeImg)){
            userInfoParam.setLargeImg(largeImg);
        }
        if(!StringUtils.isEmpty(smallImg)){
            userInfoParam.setSmallImg(smallImg);
        }
        if(!StringUtils.isEmpty(gender)){
            userInfoParam.setSmallImg(smallImg);
        }
        if(!StringUtils.isEmpty(birthday)){
            userInfoParam.setBirthday(birthday);
        }
        if(userInfoService.updateUserInfo(userInfoParam)){
            return sendResult200();
        }
        return sendResult500();
    }


    /**
     * 根据uid查询用户信息
     *
     * @param userId 用户id
     * @return
     */
//    @BoTuOperation(ApiLevel = APILevel.PUBLIC)
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public BaseResult findByUid(@RequestParam String userId) {
       return sendResult200( userInfoService.findUserInfoByUid(userId));
    }
}
